package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.McInfoListMapper;
import cc.rengu.igas.momp.common.dao.MchntInfoMapper;
import cc.rengu.igas.momp.common.dao.PlatBizTmpMapper;
import cc.rengu.igas.momp.common.dao.impl.McInfoListMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.MchntInfoMapperImpl;
import cc.rengu.igas.momp.common.dao.impl.PlatBizTmpMapperImpl;
import cc.rengu.igas.momp.common.entity.CpMcListInfoInObj;
import cc.rengu.igas.momp.common.entity.CpMcListInfoOutObj;
import cc.rengu.igas.momp.common.entity.TBMchntBaseInfo;
import cc.rengu.igas.momp.common.entity.TMPlatBizTmp;
import cc.rengu.igas.momp.common.enums.*;
import cc.rengu.igas.momp.common.model.bean.JsonTmpCpMcInfo;
import cc.rengu.igas.momp.common.util.JsonOperation;
import cc.rengu.igas.momp.facade.bean.CouponMcListInfoBean;
import cc.rengu.igas.momp.facade.request.QueryCouponMcListInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryCouponMcListInfoResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 卡券类活动列表查询功能实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/3/16 12:26
 */
public class QueryCouponMcListService extends RadpService {

    private String globalInstId;
    private String globalQueryType;
    private String globalQueryRole;
    private String globalQueryOrgId;
    /**
     * 操作人
     */
    private String globalQueryId;
    /** 操作角色 */
    /**
     * 当前系统日期
     */
    private String globalTimeStamp;

    @Override
    protected int bizInit(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {

            QueryCouponMcListInfoRequest queryCouponMcListInfoRequest = new QueryCouponMcListInfoRequest();
            ConvertUtil.convertOutput(queryCouponMcListInfoRequest);

            QueryCouponMcListInfoResponse queryCouponMcListInfoResponse = new QueryCouponMcListInfoResponse();
            queryCouponMcListInfoResponse.setHeader(queryCouponMcListInfoRequest.getHeader());
            BizResponse<QueryCouponMcListInfoResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryCouponMcListInfoResponse);

            //报文检查
            messageValidation(queryCouponMcListInfoRequest);

            String timeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
            String oprId = queryCouponMcListInfoRequest.getQueryId();

            rglog.info("机构<{}>在<{}>由<{}>开始查询卡券类营销活动列表信息流程", globalInstId, timeStamp, oprId);

            /* 检查操作员所属机构号 */
            if (StringUtil.isNullorEmpty(globalQueryOrgId)) {
                rglog.error("法人机构号<{}>的操作员<{}>的所属机构为空!", globalInstId, oprId);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }

            // 服务调用
            BizResponse<QueryCouponMcListInfoResponse> bizResponseNew = queryCouponMcListInfo(xmlTreeUtil, queryCouponMcListInfoRequest, queryCouponMcListInfoResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return CommonConstant.PROCESS_SUCCESS;
    }

    /**
     * 查询卡券类营销活动列表信息
     *
     * @param xmlTreeUtil                   内部XML树
     * @param queryCouponMcListInfoRequest  卡券营销活动列表查询接口请求对象
     * @param queryCouponMcListInfoResponse 卡券类营销活动列表查询接口应答对象
     * @return cc.rengu.oltp.service.model.BizResponse<cc.rengu.igas.momp.facade.response.QueryCouponMcListInfoResponse> 卡券类营销活动列表查询接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/29 22:16
     */
    private BizResponse<QueryCouponMcListInfoResponse> queryCouponMcListInfo(XmlTreeUtil xmlTreeUtil, QueryCouponMcListInfoRequest queryCouponMcListInfoRequest, QueryCouponMcListInfoResponse queryCouponMcListInfoResponse) throws Exception {

        BizResponse<QueryCouponMcListInfoResponse> bizResponse = new BizResponse<>();
        List<CouponMcListInfoBean> couponMcListInfoBeanList;

        List<CpMcListInfoOutObj> cpMcListInfoOutObjList = new ArrayList<>();
        PageResult pageResult;

        /* 数据库入参赋值 */
        CpMcListInfoInObj cpMcListInfoInObj = setupDatabaseInputObject(queryCouponMcListInfoRequest);

        PageInfo pageInfo = new PageInfo();
        pageInfo.setPageIndex(Integer.parseInt(queryCouponMcListInfoRequest.getPageIndex()));
        pageInfo.setPageSize(Integer.parseInt(queryCouponMcListInfoRequest.getPageSize()));

        rglog.debug("当前是<{}>查询数据", QueryRoleEnum.getByValue(globalQueryRole).getQueryRoleDesc());
        if (QueryTypeEnum.BANK_QUERY_TYPE.getQueryTypeCode().equals(globalQueryType) && QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
            //银行审批岗因为数据查询的为业务数据临时表所以单独处理
            couponMcListInfoBeanList = processTmpApsMcListInfo(queryCouponMcListInfoRequest);

            if (null == couponMcListInfoBeanList || couponMcListInfoBeanList.isEmpty()) {
                rglog.info("根据查询条件查询到0条卡券类营销活动信息!");
                queryCouponMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryCouponMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
            } else {
                queryCouponMcListInfoResponse.setTotalRows(String.valueOf(couponMcListInfoBeanList.size()));
                if (CommonConstant.ZERO == (couponMcListInfoBeanList.size() % Integer.parseInt(queryCouponMcListInfoRequest.getPageSize()))) {
                    queryCouponMcListInfoResponse.setTotalPage(String.valueOf(couponMcListInfoBeanList.size() / Math.abs(Integer.parseInt(queryCouponMcListInfoRequest.getPageSize()))));
                } else {
                    queryCouponMcListInfoResponse.setTotalPage(String.valueOf(couponMcListInfoBeanList.size() / Math.abs(Integer.parseInt(queryCouponMcListInfoRequest.getPageSize())) + 1));
                }

                /* 分页查询 */
                int fromIndex = Integer.parseInt(queryCouponMcListInfoRequest.getPageIndex()) * Integer.parseInt(queryCouponMcListInfoRequest.getPageSize());
                int endIndex = fromIndex + Integer.parseInt(queryCouponMcListInfoRequest.getPageSize());
                if (fromIndex <= couponMcListInfoBeanList.size()) {
                    List<CouponMcListInfoBean> couponMcListInfoBeanList1 = couponMcListInfoBeanList.subList(fromIndex, Math.min(endIndex, couponMcListInfoBeanList.size()));
                    queryCouponMcListInfoResponse.setMcListInfo(couponMcListInfoBeanList1);
                }
            }
        } else {
            /* 根据角色权限查询数据 */
            switch (QueryTypeEnum.getByValue(globalQueryType)) {
                case BANK_QUERY_TYPE:
                    /* 银行人员,需要拼接营销活动机构信息表查询 */

                    /* 机构编号 */
                    if (!StringUtil.isNullorEmpty(globalQueryOrgId)) {
                        cpMcListInfoInObj.setOrgId(globalQueryOrgId);
                        rglog.info("ORG_ID=<{}>", globalQueryOrgId);
                    }

                    /* 临时表数据字段1模糊查询字符串拼接 */
                    if (QueryRoleEnum.BANK_AUDIT_QYERY_ROLE.getQueryRoleCode().equals(globalQueryRole)) {
                        String bizData = "{\"inst_id\":\"" + globalInstId + "\"%";
                        cpMcListInfoInObj.setBizData1(bizData);
                    }

                    pageResult = queryMoneyOffMcListInfoByBank(cpMcListInfoInObj, pageInfo);
                    if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                        throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                    }

                    rglog.info("SIZE=<{}>", pageResult.getResult().size());

                    break;

                case MCHNT_QYERY_TYPE:
                    /* 商户人员,需要拼接营销活动商户信息表查询 */

                    /* 根据商户编号查询商圈编号 */
                    String mchntNo = queryCouponMcListInfoRequest.getQueryId();
                    String mchntArea = null;
                    TBMchntBaseInfo tbMchntBaseInfo = null;
                    if (!StringUtil.isNullorEmpty(mchntNo)) {
                        /* 查询商户基本信息表 */
                        tbMchntBaseInfo = selectMompMchntBaseInfoByPrimaryKey(globalInstId, mchntNo);
                        if (null == tbMchntBaseInfo) {
                            rglog.error("查询法人机构<{}>商户<{}>基本信息失败!", globalInstId, mchntNo);
                            throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                        } else {
                            /* 商户编号 */
                            if (!StringUtil.isNullorEmpty(mchntNo)) {
                                cpMcListInfoInObj.setMchntNo(mchntNo);
                            }

                            /* 商圈编号 */
                            mchntArea = tbMchntBaseInfo.getMchntArea();
                            if (!StringUtil.isNullorEmpty(mchntArea)) {
                                cpMcListInfoInObj.setMchntArea(mchntArea);
                            }
                        }
                    }

                    pageResult = queryCouponMcListInfoByMchnt(cpMcListInfoInObj, pageInfo, mchntArea);
                    if (null == pageResult || null == pageResult.getPageInfo() || null == pageResult.getResult()) {
                        throw new BizException(RespCodeEnum.NO_TRANS_RECORD.getRespCode(), RespCodeEnum.NO_TRANS_RECORD.getRespDesc());
                    }

                    break;

                default:
                    /* 其他查询角色不支持 */
                    rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                    throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
            }

            /* 根据取出记录条数做对应处理 */
            if (pageResult.getResult().isEmpty()) {
                rglog.info("根据查询条件查询到0条减免类营销活动信息!");

                queryCouponMcListInfoResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryCouponMcListInfoResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);

            } else {

                for (Object object : pageResult.getResult()) {
                    cpMcListInfoOutObjList.add((CpMcListInfoOutObj) object);
                }

                rglog.info("SIZE=<{}>", pageResult.getResult().size());

                couponMcListInfoBeanList = cpMcListInfoOutObjList.stream().map(this::convertCouponMcListInfo).collect(Collectors.toList());
                queryCouponMcListInfoResponse.setMcListInfo(couponMcListInfoBeanList);

                queryCouponMcListInfoResponse.setTotalPage(Integer.toString(pageResult.getPageInfo().getTotalPage()));
                queryCouponMcListInfoResponse.setTotalRows(Integer.toString(pageResult.getPageInfo().getTotalRows()));

            }
        }
        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryCouponMcListInfoResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryCouponMcListInfoResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        queryCouponMcListInfoResponse.setPageIndex(queryCouponMcListInfoRequest.getPageIndex());
        queryCouponMcListInfoResponse.setPageSize(queryCouponMcListInfoRequest.getPageSize());

        bizResponse.setResult(queryCouponMcListInfoResponse);

        return bizResponse;
    }

    /**
     * 银行人员查询数据
     *
     * @param cpMcListInfoInObj 卡券类营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/27 23:14
     */
    private PageResult queryMoneyOffMcListInfoByBank(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo) throws Exception {

        switch (QueryRoleEnum.getByValue(globalQueryRole)) {
            case BANK_BASIC_QUERY_ROLE:
                /* 银行录入岗 */
                return selectCouponMcListInfoByBankBasicRole(cpMcListInfoInObj, pageInfo);
            case BANK_PUBLISH_QYERY_ROLE:
                /* 银行发布岗 */
                return selectCouponMcListInfoByBankPublishRole(cpMcListInfoInObj, pageInfo);
            default:
                /* 其他查询角色不支持 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }
    }

    /**
     * 商户查询数据
     *
     * @param cpMcListInfoInObj 营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页请求实体类
     * @param mchntArea         商圈编号
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 13:15
     */
    private PageResult queryCouponMcListInfoByMchnt(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo, String mchntArea) throws Exception {

        switch (QueryRoleEnum.getByValue(globalQueryRole)) {
            case MCHNT_QYERY_ROLE:
                /* 商户 */
                return selectCouponMcListInfoByMchnt(cpMcListInfoInObj, pageInfo, mchntArea);
            default:
                /* 其他查询角色不支持 */
                rglog.error("当前<{}>角色的查询用户<{}>无权限操作数据!", globalQueryRole, globalQueryType);
                throw new BizException(RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespCode(), RespCodeEnum.NO_AUTHORIZATION_TO_OPER_ERROR.getRespDesc());
        }
    }

    /**
     * 银行操作员查询减免类营销活动列表数据库操作
     *
     * @param cpMcListInfoInObj 卡券类营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 00:47
     */
    private PageResult selectCouponMcListInfoByBankBasicRole(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo) throws Exception {

        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        return mcInfoListMapper.selectCouponMcListInfoByBankBasicRole(cpMcListInfoInObj, pageInfo);
    }

    /**
     * 银行发布员查询减免类营销活动列表数据库操作
     *
     * @param cpMcListInfoInObj 卡券类营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页请求实体类
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 01:13
     */
    private PageResult selectCouponMcListInfoByBankPublishRole(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo) throws Exception {

        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        return mcInfoListMapper.selectCouponMcListInfoByBankPublishRole(cpMcListInfoInObj, pageInfo);
    }

    /**
     * 商户查询数据
     *
     * @param cpMcListInfoInObj 营销活动列表查询使用实体类入参
     * @param pageInfo          数据库操作分页请求实体类
     * @param mchntArea         商圈编号
     * @return cc.rengu.oltp.utility.bean.PageResult 数据库操作分页应答实体类
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 13:13
     */
    private PageResult selectCouponMcListInfoByMchnt(CpMcListInfoInObj cpMcListInfoInObj, PageInfo pageInfo, String mchntArea) throws Exception {

        McInfoListMapper mcInfoListMapper = new McInfoListMapperImpl();
        if (StringUtil.isNullorEmpty(mchntArea)) {
            return mcInfoListMapper.selectCouponMcListInfoByMchnt(cpMcListInfoInObj, pageInfo);
        } else {
            return mcInfoListMapper.selectCouponMcListInfoByMchntWithArea(cpMcListInfoInObj, pageInfo);
        }
    }

    /**
     * 数据库实体类和接口实体类数据转换
     *
     * @param cpMcListInfoOutObj 营销活动列表查询使用实体类出参
     * @return cc.rengu.igas.momp.facade.bean.CouponMcListInfoBean 减免类营销活动列表查询接口应答对象
     * @author Jinan Liu
     * @date 2020/5/28 00:15
     */
    private CouponMcListInfoBean convertCouponMcListInfo(CpMcListInfoOutObj cpMcListInfoOutObj) {

        CouponMcListInfoBean couponMcListInfoBean = new CouponMcListInfoBean();
        try {
            /* 营销活动编号 */
            couponMcListInfoBean.setMcNo(cpMcListInfoOutObj.getMcNo());
            /* 营销活动名称 */
            couponMcListInfoBean.setMcName(cpMcListInfoOutObj.getMcName());
            /* 营销活动类型 */
            couponMcListInfoBean.setMcType(cpMcListInfoOutObj.getMcType());
            /* 营销活动状态 */
            couponMcListInfoBean.setMcStatus(cpMcListInfoOutObj.getMcStatus());
            /* 活动起始日期 */
            couponMcListInfoBean.setMcStartDate(cpMcListInfoOutObj.getMcStartDate());
            /* 活动截止日期 */
            couponMcListInfoBean.setMcEndDate(cpMcListInfoOutObj.getMcEndDate());
            /* 活动结束日期 */
            couponMcListInfoBean.setMcActualEndDate(cpMcListInfoOutObj.getMcActualEndDate());
            /* 活动预算 */
            couponMcListInfoBean.setBudget(cpMcListInfoOutObj.getBudget());
            /* 已消耗预算 */
            if (StringUtil.isNullorEmpty(cpMcListInfoOutObj.getTotalAmtAcc())) {
                couponMcListInfoBean.setBudgetUsed(CommonConstant.ZERO_COMMON_CONSTANT);
            } else {
                couponMcListInfoBean.setBudgetUsed(cpMcListInfoOutObj.getTotalAmtAcc());
            }
            /* 卡券有效起始日期 */
            if (!StringUtil.isNullorEmpty(cpMcListInfoOutObj.getCouponStartDate())) {
                couponMcListInfoBean.setCouponValidStartDate(cpMcListInfoOutObj.getCouponStartDate());
            }
            /* 卡券有效结束日期 */
            if (!StringUtil.isNullorEmpty(cpMcListInfoOutObj.getCouponEndDate())) {
                couponMcListInfoBean.setCouponValidEndDate(cpMcListInfoOutObj.getCouponEndDate());
            }
            /* 卡券总库存 */
            if (!StringUtil.isNullorEmpty(cpMcListInfoOutObj.getCouponInventory())) {
                couponMcListInfoBean.setCouponInventory(cpMcListInfoOutObj.getCouponInventory());
            }
            /* 卡券已领取张数 */
            if (!StringUtil.isNullorEmpty(cpMcListInfoOutObj.getCouponClaimed())) {
                couponMcListInfoBean.setCouponClaimed(cpMcListInfoOutObj.getCouponClaimed());
            }
            /* 卡券已核销张数 */
            if (!StringUtil.isNullorEmpty(cpMcListInfoOutObj.getCouponRedeemed())) {
                couponMcListInfoBean.setCouponRedeemed(cpMcListInfoOutObj.getCouponRedeemed());
            }
            /* 审批状态 */
            couponMcListInfoBean.setAuditStatus(cpMcListInfoOutObj.getAuditStatus());
            /* 创建人 */
            couponMcListInfoBean.setOprId(cpMcListInfoOutObj.getOprId());
            /* 创建时间 */
            couponMcListInfoBean.setCreateTime(cpMcListInfoOutObj.getCreateTime());
            /* 最后修改人 */
            if (StringUtil.isNullorEmpty(cpMcListInfoOutObj.getLastOprId())) {
                couponMcListInfoBean.setLastOprId("");
            } else {
                couponMcListInfoBean.setLastOprId(cpMcListInfoOutObj.getLastOprId());
            }
            /* 修改时间 */
            if (StringUtil.isNullorEmpty(cpMcListInfoOutObj.getUpdateTime())) {
                couponMcListInfoBean.setUpdateTime("");
            } else {
                couponMcListInfoBean.setUpdateTime(cpMcListInfoOutObj.getUpdateTime());
            }

        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换营销活动信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return couponMcListInfoBean;
    }

    /**
     * 数据库查询前赋值数据库实体类入参
     *
     * @param queryCouponMcListInfoRequest 卡券营销活动列表查询接口请求对象
     * @return cc.rengu.igas.momp.common.entity.CouponMcListInfoInObj 卡券类营销活动列表查询使用实体类入参
     * @author Jinan Liu
     * @date 2020/5/28 12:57
     */
    private CpMcListInfoInObj setupDatabaseInputObject(QueryCouponMcListInfoRequest queryCouponMcListInfoRequest) {

        CpMcListInfoInObj cpMcListInfoInObj = new CpMcListInfoInObj();

        /* 机构编码 */
        cpMcListInfoInObj.setInstId(globalInstId);
        rglog.info("INST_ID=<{}>", globalInstId);

        /* 营销活动编号 */
        if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getMcNo())) {
            cpMcListInfoInObj.setMcNo(queryCouponMcListInfoRequest.getMcNo());
        }
        /* 营销活动名称 */
        if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getMcName())) {
            cpMcListInfoInObj.setMcName(queryCouponMcListInfoRequest.getMcName());
        }
        /* 营销活动类型 */
        if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getMcType())) {
            cpMcListInfoInObj.setMcType(queryCouponMcListInfoRequest.getMcType());
        }
        /* 营销活动状态 */
        if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getMcStatus())) {
            cpMcListInfoInObj.setMcStatus(queryCouponMcListInfoRequest.getMcStatus());
        }
        /* 审批状态 */
        if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getAuditStatus())) {
            cpMcListInfoInObj.setAuditStatus(queryCouponMcListInfoRequest.getAuditStatus());
        }
        /* 当前系统日期 */
        if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getHeader().getTransDate())) {
            cpMcListInfoInObj.setTxnDate(queryCouponMcListInfoRequest.getHeader().getTransDate());
            rglog.info("TXN_DATE=<{}>", queryCouponMcListInfoRequest.getHeader().getTransDate());
        }

        return cpMcListInfoInObj;
    }

    /**
     * 使用商户编号从内管商户基本信息表查询商户所属商圈编号
     *
     * @param instId  法人机构号
     * @param mchntNo 商户编号
     * @return cc.rengu.igas.momp.common.entity.TBMchntBaseInfo 商户基本信息表T_B_MCHNT_BASE_INFO
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/5/28 13:06
     * @version 1.0.0
     */
    private TBMchntBaseInfo selectMompMchntBaseInfoByPrimaryKey(String instId, String mchntNo) throws Exception {

        MchntInfoMapper mchntInfoMapper = new MchntInfoMapperImpl();
        return mchntInfoMapper.selectMompMchntBaseInfoByPrimaryKey(instId, mchntNo);
    }

    /**
     * 银行审批岗数据查询
     *
     * @param queryCouponMcListInfoRequest 卡券活动列表查询接口请求对象
     * @return java.util.List<cc.rengu.igas.momp.facade.bean.CouponMcListInfoBean> 卡券类营销活动列表查询接口应答对象
     * @throws Exception 异常信息
     * @author Jinan Liu
     * @date 2020/6/20 16:53
     */
    private List<CouponMcListInfoBean> processTmpApsMcListInfo(QueryCouponMcListInfoRequest queryCouponMcListInfoRequest) throws Exception {

        List<CouponMcListInfoBean> couponMcListInfoBeanList;

        TMPlatBizTmp tmPlatBizTmp = new TMPlatBizTmp();
        tmPlatBizTmp.setInstId(globalInstId);
        tmPlatBizTmp.setOrgId(globalQueryOrgId);
        tmPlatBizTmp.setOprTableEname(TableNameEnum.T_M_MC_CP_INFO.getTableNameCode());

        //营销活动编号
        if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getMcNo())) {
            tmPlatBizTmp.setMcNo(queryCouponMcListInfoRequest.getMcNo());
        }
        //营销活动状态
        if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getMcStatus())) {
            tmPlatBizTmp.setMcStatus(queryCouponMcListInfoRequest.getMcStatus());
        }

        // 查询临时表获取数据
        PlatBizTmpMapper platBizTmpMapper = new PlatBizTmpMapperImpl();
        List<TMPlatBizTmp> tmPlatBizTmpList = platBizTmpMapper.selectTmpMcListInfo(tmPlatBizTmp);
        if (null != tmPlatBizTmpList && !tmPlatBizTmpList.isEmpty()) {
            couponMcListInfoBeanList = tmPlatBizTmpList.stream().map(this::convertTmpMcInfo).collect(Collectors.toList());
            rglog.debug("活动查询结果 SIZE=<{}>", couponMcListInfoBeanList.size());

            /* 代码过滤查询条件 */
            //按照营销活动名称筛选
            if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getMcName())) {
                couponMcListInfoBeanList = couponMcListInfoBeanList.stream().filter(item -> item.getMcName().contains(queryCouponMcListInfoRequest.getMcName())).collect(Collectors.toList());
            }
            //按照营销活动类型筛选
            if (!StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getMcType())) {
                couponMcListInfoBeanList = couponMcListInfoBeanList.stream().filter(item -> item.getMcType().equals(queryCouponMcListInfoRequest.getMcType())).collect(Collectors.toList());
            }
            return couponMcListInfoBeanList;
        } else {
            return null;
        }
    }

    /**
     * 转换换业务数据临时表临时数据
     *
     * @param tmPlatBizTmp 业务数据临时表
     * @return cc.rengu.igas.momp.facade.bean.CouponMcListInfoBean 卡券类营销活动列表查询接口应答对象
     * @author Jinan Liu
     * @date 2020/6/20 15:37
     */
    private CouponMcListInfoBean convertTmpMcInfo(TMPlatBizTmp tmPlatBizTmp) {

        CouponMcListInfoBean couponMcListInfoBean = new CouponMcListInfoBean();
        try {
            String mcNo;
            String mcName;
            String mcType;
            String mcStartDate;
            String mcEndDate;
            String mcActualEndDate;
            String sponsor = null;
            String budget;

            /* 根据营销活动类型解析BIZ_DATA1字段内容 */
            String mcTypeOne = tmPlatBizTmp.getMcNo().substring(2, 3);
            if (McTypeEnum.MC_TYPE_CP.getMcTypeCode().equals(mcTypeOne)) {
                JsonTmpCpMcInfo jsonTmpCpMcInfo = JsonOperation.parseCpMcInfoTmpBizData1(tmPlatBizTmp, rglog);
                mcNo = jsonTmpCpMcInfo.getMcNo();
                mcName = jsonTmpCpMcInfo.getMcName();
                mcType = jsonTmpCpMcInfo.getMcType();
                mcStartDate = jsonTmpCpMcInfo.getMcStartDate();
                mcEndDate = jsonTmpCpMcInfo.getMcEndDate();
                mcActualEndDate = jsonTmpCpMcInfo.getMcActualEndDate();
                if (!StringUtil.isNullorEmpty(jsonTmpCpMcInfo.getSponsor())) {
                    sponsor = jsonTmpCpMcInfo.getSponsor();
                }
                budget = jsonTmpCpMcInfo.getBudget();

            } else {
                rglog.error("不支持的营销活动类型<{}>,不可转换数据!", mcTypeOne);
                throw new BizException(RespCodeEnum.NOT_SUPPORTED_MC_TYPE_ERROR.getRespCode(), RespCodeEnum.NOT_SUPPORTED_MC_TYPE_ERROR.getRespDesc());
            }

            /* 营销活动编号 */
            couponMcListInfoBean.setMcNo(mcNo);
            /* 营销活动名称 */
            couponMcListInfoBean.setMcName(mcName);
            /* 营销活动类型 */
            couponMcListInfoBean.setMcType(mcType);
            /* 活动起始日期 */
            couponMcListInfoBean.setMcStartDate(mcStartDate);
            /* 活动截止日期 */
            couponMcListInfoBean.setMcEndDate(mcEndDate);
            /* 活动结束日期 */
            couponMcListInfoBean.setMcActualEndDate(mcActualEndDate);
            /* 出资方 */
            if (!StringUtil.isNullorEmpty(sponsor)) {
                couponMcListInfoBean.setSponsor(sponsor);
            }
            /* 活动预算 */
            couponMcListInfoBean.setBudget(budget);
            /* 已消耗预算 */
            couponMcListInfoBean.setBudgetUsed(CommonConstant.ZERO_AMOUNT);
            /* 审批状态 */
            couponMcListInfoBean.setAuditStatus(tmPlatBizTmp.getAuditStatus());
            /* 创建人 */
            couponMcListInfoBean.setOprId(tmPlatBizTmp.getOprId());
            /* 创建时间 */
            couponMcListInfoBean.setCreateTime(tmPlatBizTmp.getCreateTime());
            /* 最后修改人 */
            couponMcListInfoBean.setLastOprId(tmPlatBizTmp.getLastOprId());
            /* 修改时间 */
            couponMcListInfoBean.setUpdateTime(tmPlatBizTmp.getLastUpdateTime());

        } catch (Exception e) {
            rglog.error("转换营销活动临时表信息异常,异常信息:<{}>", StringUtil.ExceptionToString(e));
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return couponMcListInfoBean;
    }

    /**
     * 请求报文体内容检查
     *
     * @param queryCouponMcListInfoRequest 卡券类活动列表查询接口请求对象
     * @author Jinan Liu
     * @date 2020/6/20 09:55
     */
    private void messageValidation(QueryCouponMcListInfoRequest queryCouponMcListInfoRequest) {

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getHeader().getTransDate())) {
            rglog.error("交易日期必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getHeader().getTransTime())) {
            rglog.error("交易时间必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getHeader().getInstId())) {
            rglog.error("法人机构号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalInstId = queryCouponMcListInfoRequest.getHeader().getInstId();
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getHeader().getTraceNo())) {
            rglog.error("流水号必须上送!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getQueryType())) {
            rglog.error("操作员类型为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryType = queryCouponMcListInfoRequest.getQueryType();
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getQueryId())) {
            rglog.error("操作员ID为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryId = queryCouponMcListInfoRequest.getQueryId();
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getQueryRole())) {
            rglog.error("操作员角色为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryRole = queryCouponMcListInfoRequest.getQueryRole();
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getQueryOrgId())) {
            rglog.error("操作员所属法人机构为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            globalQueryOrgId = queryCouponMcListInfoRequest.getQueryOrgId();
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getPageIndex())) {
            rglog.error("当前页数为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            int pageIndex = Integer.parseInt(queryCouponMcListInfoRequest.getPageIndex());
            if (CommonConstant.ZERO > pageIndex) {
                rglog.error("当前查询页数必须大于0! PAGE_INDEX=<{}>", pageIndex);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }

        if (StringUtil.isNullorEmpty(queryCouponMcListInfoRequest.getPageSize())) {
            rglog.error("页显示记录数为空!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        } else {
            int pageSize = Integer.parseInt(queryCouponMcListInfoRequest.getPageSize());
            if (CommonConstant.ONE > pageSize) {
                rglog.error("每页记录数必须大于等于1! PAGE_SIZE=<{}>", pageSize);
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
        }
        globalTimeStamp = new SimpleDateFormat(CommonConstant.TIMESTAMP_FORMAT).format(new Date());
    }
}
